<template>
  <div>
    <el-dialog :visible.sync="visible"
               title="慢病服务医院管理"
               width="50%"
               :before-close="modalClose"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-button type="primary"
                 @click="handleAdd">新 增
      </el-button>
      <element-table class="mt-20"
                     :columns="columns"
                     :data="tableData"
                     v-loading="loading"
                     pager
                     :pageTotal="pageTotal"
                     :pageIndex="pageIndex"
                     :pageSize="pageSize"
                     @handleChangePage="handleChangePage">

        <template v-slot:option="{row}">
          <el-button type="primary"
                     @click="handleOption('edit', row)">编辑
          </el-button>
          <el-button type="danger"
                     @click="handleOption('del', row)">删除
          </el-button>
        </template>
      </element-table>


    </el-dialog>
    <!-- 慢病医院管理编辑 -->
    <slowupdate-dialog :slowHospitalData="slowHospitalData"
                       v-if="showSlowHospitalDialog"
                       :visible.sync="showSlowHospitalDialog"/>
  </div>
</template>

<script>
import ElementTable from '@/base/ElementTable'
import slowHospitalUpdateDialog from './slowHospitalUpdateDialog'

export default {
  name: "slowHospitalManageDialog",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      slowHospitalData: {},
      showSlowHospitalDialog: false,
      companyId: '',
      tableData: [],
      pageTotal: 0,
      pageIndex: 1,
      pageSize: 20,
      loading: false,
      columns: [
        {
          label: '医院名称',
          prop: 'hospitalDto.name',
          align: "center"
        }, {
          label: '医院等级',
          prop: 'hospitalDto.level',
          align: "center"
        }, {
          label: '地址',
          prop: 'hospitalDto.address',
          align: "center"
        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
        }
      ]
    }
  },
  created() {
    this.companyId = this.dataObj.id
    this.slowHospitalData.companyId = this.dataObj.id
    this.getList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    //新增
    handleAdd() {
      this.slowHospitalData={}
      this.slowHospitalData.companyId=this.dataObj.id
      this.showSlowHospitalDialog = true
      this.slowHospitalData.organization = 'bclient'//机构
    },
    async handleOption(op, row) {
      switch (op) {
        case 'edit':
          this.slowHospitalData = row
          this.showSlowHospitalDialog = true;
          this.slowHospitalData.organization = 'bclient'//机构
          break;
        case 'del':
          this.$confirm('是否要确认?', '提示', {
            type: 'warning'
          }).then(() => {
            this.post(this.api.company.deleteServiceHospital, {
              id: row.id
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('删除成功')
                this.getList()
              }
            })
          })
          break;
      }
    },
    getList() {
      this.post(this.api.company.getServiceHospitalList, {
        pageIndex: this.pageIndex - 1,
        pageSize: this.pageSize,
        companyId: this.companyId
      }).then((res) => {
        if (res.data.code === '200') {
          this.tableData = res.data.object
          this.pageTotal = res.data.totalNum
        }
      })
    },
    // 分页
    handleChangePage(page) {
      this.pageIndex = page;
      this.getList();
    },
  },
  components: {
    'element-table': ElementTable,
    'slowupdate-dialog': slowHospitalUpdateDialog
  }
}
</script>

<style scoped>

</style>
